﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YNCNumberSys.db;
using System.Data.SqlClient;
using myDBDataAdapter = System.Data.SqlClient.SqlDataAdapter;
using myDBConnection = System.Data.SqlClient.SqlConnection;
using myDBTransaction = System.Data.SqlClient.SqlTransaction;
using myDBCommand = System.Data.SqlClient.SqlCommand;

namespace YNCNumberSys
{
    /// <summary>
    /// XSL 核查了一遍20121121.
    /// </summary>
    public partial class ListInfoCancel : Form
    {
        CommandManager commandManager = new CommandManager();
        ConnectionManager connectionManager = new ConnectionManager();
        private System.Data.DataTable dt_编码位数;

        /// <summary>
        /// 排好序的设备表
        /// </summary>
        private System.Data.DataTable m_Table;

        public System.Data.DataTable Table
        {
            get { return m_Table; }
            set { m_Table = value; }
        }
        public ListInfoCancel()
        {
            InitializeComponent();
        }
        public void DataBinding()
        {
            c1FlexGrid1.DataSource = m_Table;
            //for (int r = 1; r < c1FlexGrid1.Rows.Count; r++)
            //{
            //    c1FlexGrid1[r, 0] = r;
            //}
            c1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw;
            c1FlexGrid1.Refresh();
            
            //string result = DataTable2String(m_Table);
            //Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(result);
        }

        public static string DataTable2String(DataTable dataTable)
        {
            StringBuilder sb = new StringBuilder();
            if (dataTable != null)
            {
                string seperator = " | ";

                #region get min length for columns
                System.Collections.Hashtable hash = new System.Collections.Hashtable();
                foreach (DataColumn col in dataTable.Columns)
                    hash[col.ColumnName] = col.ColumnName.Length;
                foreach (DataRow row in dataTable.Rows)
                    for (int i = 0; i < row.ItemArray.Length; i++)
                        if (row[i] != null)
                            if (((string)row[i]).Length > (int)hash[dataTable.Columns[i].ColumnName])
                                hash[dataTable.Columns[i].ColumnName] = ((string)row[i]).Length;
                int rowLength = (hash.Values.Count + 1) * seperator.Length;
                foreach (object o in hash.Values)
                    rowLength += (int)o;
                #endregion get min length for columns

                sb.Append(new string('=', (rowLength - " DataTable ".Length) / 2));
                sb.Append(" DataTable ");
                sb.AppendLine(new string('=', (rowLength - " DataTable ".Length) / 2));
                if (!string.IsNullOrEmpty(dataTable.TableName))
                    sb.AppendLine(String.Format("{0,-" + rowLength + "}", String.Format("{0," + ((rowLength + dataTable.TableName.Length) / 2).ToString() + "}", dataTable.TableName)));

                #region write values
                foreach (DataColumn col in dataTable.Columns)
                    sb.Append(seperator + String.Format("{0,-" + hash[col.ColumnName] + "}", col.ColumnName));
                sb.AppendLine(seperator);
                sb.AppendLine(new string('-', rowLength));
                foreach (DataRow row in dataTable.Rows)
                {
                    for (int i = 0; i < row.ItemArray.Length; i++)
                    {
                        sb.Append(seperator + String.Format("{0," + hash[dataTable.Columns[i].ColumnName] + "}", row[i]));
                        if (i == row.ItemArray.Length - 1)
                            sb.AppendLine(seperator);
                    }
                }
                #endregion write values

                sb.AppendLine(new string('=', rowLength));
            }
            else
                sb.AppendLine("================ DataTable is NULL ================");

            return sb.ToString();
        }

        private void btn_查询_Click(object sender, EventArgs e)
        {
            System.Data.DataTable dt_结果信息 = null;
            System.Data.DataTable dt = null;
            //c1FlexGrid1.Clear();
            myDBConnection conn = connectionManager.getConnection();
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            myDBTransaction trans = null;
            trans = conn.BeginTransaction();
            try
            {
                string sql = "select * from 编码位数 where 1=1";
                dt_编码位数 = commandManager.ExecuteNonQuery(conn, trans, sql, "编码位数");

                sql = "select * from 图号作废表 where 1=1 ";
                if (tbx_机床型号.Text != string.Empty)//19-1-18
                {
                    sql = sql + " and 机床型号 like '%" + tbx_机床型号.Text + "%'";
                }
                dt_结果信息 = commandManager.ExecuteNonQuery(conn, trans, sql, "图号作废表");
                dt = new System.Data.DataTable();
                dt = dt_结果信息.Copy();
                dt.Columns.Remove("图纸编号");
                dt.Columns.Remove("ID");
                //dt.Columns.Remove("名称");
                //dt.Columns.Remove("规格");
                dt.Columns.Remove("外协");
                dt.Columns.Remove("外协单位");
                dt.Columns.Remove("作废");
                dt.Columns.Remove("零件序号");
                dt.Columns.Remove("图纸类型");
                dt.Columns.Add("图纸编号");
                dt.Columns["图纸编号"].DataType = System.Type.GetType("System.String");
                dt.Columns["图纸编号"].SetOrdinal(0);
            }
            catch (Exception ex)
            {
                if (trans != null)
                {
                    trans.Rollback();
                }
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
                if(dt_结果信息!=null && dt != null)
                for (int ir = 0; ir < dt_结果信息.Rows.Count; ir++)
                {
                    dt.Rows[ir]["图纸编号"] = dt_结果信息.Rows[ir]["图纸类型"].ToString() + dt_结果信息.Rows[ir]["图纸编号"].ToString().PadLeft(int.Parse(dt_编码位数.Rows[0]["编码位数"].ToString()), '0');
                }
                this.Table = dt.Copy();
                this.DataBinding();
            }

            
        }
    }
}
